
    import React, { useContext } from 'react';

    export type Permissions=Record<string, boolean>;
    export type  AccessContextType={
      check: (code: string) => boolean,
      permissions:Permissions,
    }
    
    const AccessContext = React.createContext<AccessContextType>({} as AccessContextType);
    
    export type AccessProviderProps={
      children:React.ReactNode,
      permissions: Record<string, boolean>,
    }
    
    export type AccessType={
      fallback?:React.ReactNode,
      children:React.ReactNode,
      code: string,
    }
    //Provider
    export function AccessProvider({ children, permissions }: AccessProviderProps) {
    
      //根据code判断权限
      const check = (code: string) => {
        return !!permissions?.[code];
      };
    
      return (
        <AccessContext.Provider value={{ check, permissions}}>
          {children}
        </AccessContext.Provider>
      );
    }
    
    export function useAccess():AccessContextType {
      const value = useContext(AccessContext);
      return value;
    }
    
    export function Access({ fallback, children, code }: AccessType) {
      const { check } = useAccess();
    
      if (!check(code)) {
        return fallback ? fallback : null;
      }
    
      return children;
    }